package com.murphy.mq.activemq.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.murphy.system.dao.UserMapper;
import com.murphy.system.model.User;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

@Service
public class QueneProducer {

    @Autowired
    JmsTemplate jmsTemplate;

    @Autowired
    ActiveMQQueue activeMQQueue;

    @Autowired
    UserMapper userMapper;

    public void QueneProducer(){
        for (int i = 0; i < 10; i++) {
            System.out.println("开始发送小时===》"+i);

            jmsTemplate.send(activeMQQueue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    User user = userMapper.queryUserByLoginName("admin");
                    Gson gson = new GsonBuilder().create();
                    TextMessage textMessage = session.createTextMessage(gson.toJson(user));
                    return textMessage;
                }
            });
            System.out.println("第"+i+"条消息发送完成");
        }
    }

}
